<script setup lang="ts">
import { computed, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import BottomNav from '@/components/BottomNav.vue';
import { useWebSocketStore } from '@/stores/websocket';

const route = useRoute();
const router = useRouter();
const showNav = computed(() => !['/chat/:friendId', '/login', '/register'].includes(route.path));
const websocketStore = useWebSocketStore();

onMounted(() => {
  const userId = localStorage.getItem('userId');
  if (userId) {
    websocketStore.connect(userId); // 如果用户已登录，自动连接 WebSocket
  } else if (route.path === '/') {
    router.push('/login');
  }
});
</script>

<template>
  <div class="container">
    <router-view />
    <BottomNav v-if="showNav" />
  </div>
</template>

<style scoped>
.container {
  flex: 1;
  padding: 20px;
}

header {
  line-height: 1.5;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }
}
</style>
